package com.hula.onion.book.server.spider.platforms.jrjc;

import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.common.SpiderCommon;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import com.hula.onion.book.server.utils.HttpUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by work on 2018/3/4.
 */
@Component("jRJCSpider")
public class JinRiJieCaiSpider extends SpiderCommon {
    @Autowired
    private JinRiJieCaiSpiderHelper spiderHelper;

    private static final String LOGIN_URL = "https://www.51jiecai.com/passport/login.html?targetUrl=http://www.51jiecai.com/user/newAccount.html";

    @Override
    public CommonResponse execute() {
        String loginResult = HttpUtils.post(LOGIN_URL,
                spiderHelper.installRequestParams(loginForm),
                getHttpContext());
        if (StringUtils.isEmpty(loginResult)) {
            return new CommonResponse(ResponseStatusEnum.FAIL);
        }
        if (loginResult.contains("用户名密码不匹配")) {
            return new CommonResponse(ResponseStatusEnum.ACCOUNT_ERROR);
        }
        return spiderHelper.parseAccountBalance(loginResult);
    }
}
